package com.situ.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.github.pagehelper.PageInfo;
import com.situ.bean.APIResult;
import com.situ.bean.Staff;
import com.situ.service.StaffService;

@RestController
@RequestMapping("/api/staffs")
@CrossOrigin(origins = { "http://127.0.0.1:8848" })
public class StaffController {

	@Autowired
	private StaffService staffService;

	@PostMapping()
	public APIResult add(@RequestBody Staff staff) {

		APIResult apiResult = new APIResult();
		staff = staffService.add(staff);
		apiResult.setStatus(201);
		apiResult.setData(staff);
		return apiResult;
	}

	@DeleteMapping
	public APIResult remove(Integer id) {

		Staff staff = staffService.remove(id);
		APIResult apiResult = new APIResult();
		if (staff != null) {
			apiResult.setStatus(204);
			apiResult.setData(staff);
		} else {
			apiResult.setStatus(404);
			apiResult.setMsg("删除的员工不存在");
		}
		return apiResult;
	}

	@PutMapping()
	public APIResult edit(@RequestBody Staff staff) {

		staff = staffService.edit(staff);
		APIResult apiResult = new APIResult();
		if (staff != null) {
			apiResult.setStatus(201);
			apiResult.setData(staff);
		} else {
			apiResult.setStatus(404);
			apiResult.setMsg("修改的员工不存在！！");
		}
		return apiResult;
	}

//	@GetMapping("/{id}")
//	public APIResult get(@PathVariable("id") Integer id) {
//
//		Staff staff = staffService.getById(id);
//		APIResult apiResult = new APIResult();
//		if (staff != null) {
//			apiResult.setStatus(200);
//			apiResult.setData(staff);
//		} else {
//			apiResult.setStatus(404);
//			apiResult.setMsg("该员工不存在");
//		}
//		return apiResult;
//	}

	@GetMapping()
	public APIResult get(Integer page, Integer limit, Staff staff) {
		// List list = staffService.getAll();
		PageInfo pageInfo = staffService.getByPage(page, limit, staff);
		APIResult apiResult = new APIResult();
		if (pageInfo.getTotal() > 0) {
			apiResult.setStatus(200);
			apiResult.setData(pageInfo);
		} else {
			apiResult.setStatus(404);
			apiResult.setMsg("未查到任何记录！！");
			apiResult.setData(pageInfo);
		}
		return apiResult;
	}
}
